package controllers.manage.curriculum;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.curriculum.Curriculum;
import models.venue.curriculum.CurriculumType;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程管理
 * Created by hanbin on 17/5/23.
 */
@With(BaseController.class)
public class CurriculumCtrl extends Controller {

    /**
     * 跳转到课程管理页面
     */
    public static void index(Integer pageNumber, String curriculumName, Long curriculumType){

        Merchant merchant = Secure.getLoginUser().merchant;

        // ------测试
        if(merchant == null) merchant = Merchant.findById(1l);

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtil.isNotBlank(curriculumName)) {
            searchMap.put("curriculumName", "%"+curriculumName+"%");
        }

        if(curriculumType != null) {
            searchMap.put("curriculumType", curriculumType);
        }

        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        List<CurriculumType> curriculumTypes = CurriculumType.findAllCurriculumTypeByMerchant(merchant.id);

        flash.put("menuId", "12");

        JPAExtPaginator<Curriculum> resultPage = Curriculum.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, curriculumTypes, curriculumName, curriculumType);
    }

    /**
     * 跳转到新增课程页面
     */
    public static void add(){
        Merchant merchant = Secure.getLoginUser().merchant;

        // ------测试
        if(merchant == null) merchant = Merchant.findById(1l);

        List<CurriculumType> curriculumTypes = CurriculumType.findAllCurriculumTypeByMerchant(merchant.id);
        render(curriculumTypes);
    }

    /**
     * 跳转到修改课程页面
     */
    public static void modify(long id, Integer pageNumber){
        Merchant merchant = Secure.getLoginUser().merchant;

        // ------测试
        if(merchant == null) merchant = Merchant.findById(1l);

        List<CurriculumType> curriculumTypes = CurriculumType.findAllCurriculumTypeByMerchant(merchant.id);
        Curriculum curriculum = Curriculum.findById(id);
        render(curriculum, pageNumber, curriculumTypes);
    }


    /**
     * 创建课程
     */
    public static void create(Curriculum curriculum){

        Merchant merchant = Secure.getLoginUser().merchant;

        // ------测试
        if(merchant == null) merchant = Merchant.findById(1l);

        curriculum.merchant = merchant;

        curriculum.deleted = DeletedStatus.UN_DELETED;
        curriculum.save();
        flash.put("msg" , "课程创建成功");
        redirect("/BS0016/admin/curriculum/index/1");
    }

    /**
     * 修改课程
     */
    public static void update(Long id, Curriculum curriculum, Integer pageNumber){
        Curriculum.update(id , curriculum);
        flash.put("msg" , "课程修改成功");
        redirect("/BS0016/admin/curriculum/index/"+pageNumber);
    }

    /**
     * 删除课程
     */
    public static void delete(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Curriculum curriculum = Curriculum.findById(id);
        curriculum.deleted= DeletedStatus.DELETED;
        curriculum.save();
        flash.put("msg" , "课程删除成功");
        redirect("/BS0016/admin/curriculum/index/"+pageNumber);
    }

}
